#!/bin/bash
#

##清理日志，保留最近3天的日志

now_time=$(date +%s)

d=3
y=86400
s=$((d*y))
#echo $s

ls /opt/log | while read line; do
    #echo $line
    year=$(echo $line | awk -F. '{print $1}' | awk -F- '{print $2}')
    month=$(echo $line | awk -F. '{print $1}' | awk -F- '{print $3}')
    day=$(echo $line | awk -F. '{print $1}' | awk -F- '{print $4}')
    date=${year}-${month}-${day}
    #echo $date
    file_time=$(date -d "$date" +%s)
    
    cha=$((now_time - file_time))
    #echo $cha
    if [ ${cha} -gt $s ]; then
	rm -f "/opt/log/$line"
	#echo $?
	echo "文件$line超过$d天，已被删除" 	
    fi 
   
done
